<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\Facades\Storage;

class CommonController extends Controller
{
    /**
     * 初始化控制器
     */
    public function __construct()
    {

    }

    /**
     * Handle the incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {

    }

    public function upload_image(Request $request)
    {
        // 获取文件路径
        $file = $request->file('file');
        if (!$file) {
            $this->error('未上传图片');
        }

        // 判断上传图片的后缀
        if (!in_array($file->getClientOriginalExtension(), array('jpg', 'png', 'bmp', 'jpeg', 'gif'))) {
            $this->error('上传图片格式受限制');
        }

        // 判断上传图片的大小
        if ($file->getClientSize() > 5242880) {
            $this->error('上传图片超出大小5M');
        }

        $destDir = date('Ymd');
        $path = Storage::disk('uploads')->put($destDir, $file);

        $path = Storage::disk('uploads')->url($path);
        $this->success(['url' => $path, 'full_url' => replace_imgs_url($path)]);
    }
}
